﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTSBLL;
using TTSModel;
using System.IO;
using System.Text;
using System.Drawing;
public partial class ReportingView_PackageInfoQuery : System.Web.UI.Page
{
    private Package packageBLL;
    private IList<PackageInfo> packageInfoList;
    private DateTime startTime
    {
        get { return DateTime.Parse(ViewState["startTime"].ToString()); }
        set { ViewState["startTime"] = value; }
    }
    private DateTime endTime
    {
        get { return DateTime.Parse(ViewState["endTime"].ToString()); }
        set { ViewState["endTime"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GridView1.PagerSettings.Visible = false;//隐藏自带的分页   
        }
    }
    public ReportingView_PackageInfoQuery()
    {
        packageBLL = new Package();
        packageInfoList=new List<PackageInfo>();
    }
    protected void Check_Click(object sender, EventArgs e)
    {
        
        BindData();
        this.ExportToExcel.Visible = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/search/searchMainHtml.aspx");
    }
    private void BindData()
    {
        if(RadioButton1.Checked)
        {
            packageInfoList = packageBLL.Package_GetListbyKCCSN(this.TextBox3.Text.Trim());
        }
       if(RadioButton2.Checked)
       {
           startTime = DateTime.Parse(this.TextBox1.Value.Trim());
           endTime = DateTime.Parse(this.TextBox2.Value.Trim());
           packageInfoList = packageBLL.PackageInfo_GetbyDT(this.DropDownList1.SelectedValue, startTime, endTime);
          
       }
       this.GridView1.DataSource = packageInfoList;
        this.GridView1.DataBind();
        this.ddlCurrentPage.Items.Clear();
        for (int i = 1; i <= this.GridView1.PageCount; i++)
        {
            this.ddlCurrentPage.Items.Add(i.ToString());
        }
        this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
    }

    /// <summary>
    /// 当选择下一页或者上一页后，再点击导出excel时，需要调用该方法，否则报“DropDownList1”有一个无效 SelectedIndex,因为它不在项目列表中。
    /// </summary>
    private void BindDataNoPage()
    {
        if (RadioButton1.Checked)
        {
            packageInfoList = packageBLL.Package_GetListbyKCCSN(this.TextBox3.Text.Trim());
        }
        if (RadioButton2.Checked)
        {
            startTime = DateTime.Parse(this.TextBox1.Value.Trim());
            endTime = DateTime.Parse(this.TextBox2.Value.Trim());
            packageInfoList = packageBLL.PackageInfo_GetbyDT(this.DropDownList1.SelectedValue, startTime, endTime);

        }
        this.GridView1.DataSource = packageInfoList;
        this.GridView1.DataBind();
        this.ddlCurrentPage.Items.Clear();
        for (int i = 1; i <= this.GridView1.PageCount; i++)
        {
            this.ddlCurrentPage.Items.Add(i.ToString());
        }
      
    }
  
    /// <summary>
    /// 重新绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;      
        BindData();
    }
    protected void lnkbtnFrist_Click(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = 0;
        BindData();

    }
    protected void lnkbtnPre_Click(object sender, EventArgs e)
    {
        if (this.GridView1.PageIndex > 0)
        {
            this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
            BindData();
        }
    }
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        if (this.GridView1.PageIndex < this.GridView1.PageCount)
        {
            this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
            BindData();
        }
    }
    protected void lnkbtnLast_Click(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = this.GridView1.PageCount;
        BindData();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
       // Export("application/ms-excel", "整机包装表格.xls");
        Btn_Excel_Click();
    }
   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);
        //遍历所有行设置边框样式
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border-color:Black";
        }
      
    }
    protected void Btn_Excel_Click()
    {
        //导出全部数据，取消分页
        GridView1.AllowPaging = false;
        GridView1.ShowFooter = false;
      //  BindData();
        BindDataNoPage();
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出" + System.DateTime.Now.Date.ToString("yyyyMMdd")) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文

        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        this.EnableViewState = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        ClearControls(GridView1);
        this.GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

        //还原分页显示
        GridView1.AllowPaging = true;
        GridView1.ShowFooter = true;
        BindData();
    }

    /// <summary>
    /// GridView如果需要实现导出Excel功能，则该函数需要重载
    /// </summary>
    /// <param name="control"></param>
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for
    }

    /// <summary>
    /// 清除控件中的所有控件，以便导出Excel
    /// </summary>
    /// <param name="control"></param>
    private void ClearControls(Control control)
    {
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
        return;
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        this.TextBox1.Value = "";
        this.TextBox2.Value = "";
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        this.TextBox3.Text = "";
    }
}